package com.xipu.read.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.xipu.entity.Student;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * description: 监听器
 * @author: gql
 * @date: 2022/04
 */
@Slf4j
public class ExcelListener extends AnalysisEventListener<Student> {

    // 创建list集合封装最终的数据
    List<Student> list = new ArrayList<Student>();
    int i = 2;

    /**
     * 读表头
     * @param headMap Excel中表头信息
     * @param context
     */
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        log.info("表头信息：" + headMap);
    }

    /**
     * 从第二行开始,一行一行读内容
     * @param user Excel中每行内容
     * @param analysisContext
     */
    @Override
    public void invoke(Student user, AnalysisContext analysisContext) {
        log.info("读取" + i++ + "行：" + user);
        list.add(user);
    }


    /**
     * 读取完成后执行
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("读取完毕!");
    }
}
